<?php
    /**
    * Handle file upload
    */
    if (!defined('EXPONENT')) exit('');

    if ((exponent_users_isAdmin() ||  exponent_permissions_check(array("administrate"),$loc)))
    {  
        $id=0;
        try{
            require_once(BASE."modules/profilemodule/uploader.php");
            $zip = new ZipArchive;
            $allowedExtensions = array();
            // max file size in bytes
            $sizeLimit = 100 * 1024 * 1024;
            $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
            // check folder exist or not
            if (!is_dir(BASE."tmp/mail_template"))
                mkdir(BASE."tmp/mail_template",intval(DIR_DEFAULT_MODE_STR));
            if (!is_dir(BASE."tmp/mail_template/zip"))
                mkdir(BASE."tmp/mail_template/zip",intval(DIR_DEFAULT_MODE_STR));

            $path_zip = BASE."tmp/mail_template/zip/";
            $result = $uploader->handleUpload($path_zip,true);
            $name=$uploader->getUploadName();
            $res = $zip->open($path_zip.$name);
            $dir_name=str_replace('/','',$zip->getNameIndex(0));     
            $template_dir= BASE.'modules/mailsystemmodule/template';
            $new_dir=BASE.'modules/mailsystemmodule/template/'.$dir_name;
            
            //extractTo mailsystem template
            if ($res === TRUE) 
                $zip->extractTo($template_dir);
            if (is_dir($new_dir))
            {
                
               $num=rand(0,999);
               rename($template_dir.'/'.$dir_name,$template_dir.'/'.$dir_name.$num) ;
               $new_dir.=$num;
               $dir_name.=$num;                            
            }
            /*INI file sample*/
            /*
            [theme]
            name=New theme
            author=Sonhb
            group=default
            description=
            content="theme.tpl"
            [include]
            */
            /*INI*/
            $ini_array = parse_ini_file($new_dir."/template.ini", true);
            $theme_details= $ini_array['theme'];      
            $main_content= new template('mailsystemmodule','_template_manage_edit_content');
            $file=$new_dir.'/'.$theme_details['content'];
            $image_path='modules/mailsystemmodule/template/'.$dir_name.'/images/';
            $main_content->assign('file',$file);
            $main_content->assign('image_path',$image_path);
            $theme_details['content_html']=$main_content->render();    

            if($theme_details['name']&&$theme_details['content_html'])
            {
                //Insert new template
                $content->name=$theme_details['name'];
                $content->dir=$dir_name;
                $content->author=$theme_details['author'];
                $content->description=$theme_details['description'];
                $content->group=$theme_details['group'];
                $content->content=$theme_details['content_html'];
                $content->support_file=$theme_details['support_file']?$theme_details['support_file']:0;
                $content->support_param=$theme_details['support_param']?$theme_details['support_param']:0;
                $content->date=time();   
                $id=$db->insertObject($content,'mailsystem_template');

                $result['success']=true;
                $result['id']=$id;
            }
            else
            {
                $result['success']=false;
                $result['error']='File not template install';
            }
        }
        catch(Exception $e)
        {
            $result['success']=true;
            $result['error']=$e;
        }
        //Delete temp database if have error
        if($result['success']==false && $id>0)
        {
            $db->delete('mailsystem_template', 'id = '.$id);
        }
       // echo json_encode($result);
        echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
    }

?>